package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.SearchAdapter;
import com.wnhz.work.yz.bean.Finish1;
import com.wnhz.work.yz.bean.SearchBean;
import com.wnhz.work.yz.utils.LogUtil;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class SearchActivity extends BaseActivity implements View.OnClickListener {
    private EditText et_search;
    private String searchStr = "";
    private RecyclerView search_recycler;
    private LinearLayout search_type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        EventBus.getDefault().unregister(this);//反注册EventBus
       // MyApplication.getInstance().addDestoryActivity(this, "search");
        initData();
    }  @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Finish1 event) {
        finish();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);//反注册EventBus
        super.onDestroy();
    }

    private void initData() {
        et_search.setImeOptions(EditorInfo.IME_ACTION_SEND);
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    searchStr = et_search.getText().toString();
                    if (searchStr.equals("")) {
                        search_type.setVisibility(View.VISIBLE);
                        search_recycler.setVisibility(View.GONE);
                    } else {
                        search_type.setVisibility(View.GONE);
                        search_recycler.setVisibility(View.VISIBLE);
                    }
                    upSearch();
                    return true;
                }
                return false;
            }
        });
    }

    List<SearchBean.InfoEntity> infos = new ArrayList<>();

    private void upSearch() {
        if (searchStr.trim().equals("")) {
            MyToast("请输入内容");
            return;
        }
        Map<String, Object> map = new Hashtable<>();
        map.put("keywords", searchStr);
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.API_SEARCH, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtil.e("----", result);
                Gson gson = new Gson();
                SearchBean sea = gson.fromJson(result, SearchBean.class);
                infos.clear();
                if ("1".equals(sea.getResult())) {
                    infos = sea.getInfo();
                }
            }

            @Override
            public void onFinished() {
                setData();
                super.onFinished();
            }


        });
    }

    SearchAdapter searchAdapter;

    private void setData() {

        searchAdapter = new SearchAdapter(this, infos, search_recycler);
        search_recycler.setAdapter(searchAdapter);

    }

    private LinearLayoutManager linearLayoutManager;

    private void initView() {
        search_type = (LinearLayout) findViewById(R.id.search_type);
        findViewById(R.id.cancel).setOnClickListener(this);
        findViewById(R.id.type_0).setOnClickListener(this);
        findViewById(R.id.type_1).setOnClickListener(this);
        findViewById(R.id.type_2).setOnClickListener(this);
        findViewById(R.id.type_3).setOnClickListener(this);
        findViewById(R.id.type_4).setOnClickListener(this);
        et_search = (EditText) findViewById(R.id.et_search);
        search_recycler = (RecyclerView) findViewById(R.id.search_recycler);
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

        search_recycler.setLayoutManager(linearLayoutManager);
        search_recycler.setHasFixedSize(true);
        search_recycler.setItemAnimator(new DefaultItemAnimator());

        //=========================


        //


    }

    /**
     * --type 0家居馆 1整体卫浴 2工业化内装 3文库 4视频 不传查全部--
     * --Api_search--
     */
    @Override
    public void onClick(View v) {
        searchStr = et_search.getText().toString();
        Intent intent = new Intent(this, OtherSearchActivity.class);

        switch (v.getId()) {
            case R.id.cancel:
                finish();
                break;
            case R.id.type_0:
                // upSearch();
                intent.putExtra("searchStrHint", "家居馆");
                intent.putExtra("type", "0");
                startActivity(intent);
                break;
            case R.id.type_1:
                //  upSearch();
                intent.putExtra("type", "1");
                intent.putExtra("searchStrHint", "整体卫浴");
                startActivity(intent);
                break;
            case R.id.type_2:
                //  upSearch();
                intent.putExtra("type", "2");
                intent.putExtra("searchStrHint", "工业化内装");
                startActivity(intent);
                break;
            case R.id.type_3:
                // upSearch();
                intent.putExtra("type", "3");
                intent.putExtra("searchStrHint", "文库");
                startActivity(intent);
                break;
            case R.id.type_4:
                //  upSearch();
                intent.putExtra("type", "4");
                intent.putExtra("searchStrHint", "视频");
                startActivity(intent);
                break;
        }
    }
}
